package com.bawei.library_network

import android.content.Context

import com.bawei.library_network.interceptor.*
import com.bawei.library_network.newFactory.LiveDataCallAdapterFactory
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit

/**

 * @Author : ytx

 * @Time : On 2023/5/20 08:11

 * @Description : RetrofitManager

 */
class RetrofitManager {
    companion object{
        fun createCache(): Cache {
            var file = File("/sdcard/Pictures")//缓存路径，不标准
            return Cache(file,10*1024*1024)//10M
        }
        fun getRetrofit():Retrofit{
           var client = OkHttpClient.Builder()
               //.retryOnConnectionFailure(true)//超时重连，默认1次，多次使用拦截器
               .cache(createCache())//配置缓存路径
               .readTimeout(30,TimeUnit.SECONDS)
               .writeTimeout(30,TimeUnit.SECONDS)
               .connectTimeout(30,TimeUnit.SECONDS)
//               .addInterceptor(RequestCacheInterceptor())//网络请求前读取缓存
//               .addNetworkInterceptor(ResponseCacheInterceptor())//网络请求后缓存数据
//               .addInterceptor(RetryInterceptor(3))
               .addInterceptor(HeaderInterceptor())
               .addInterceptor(BasicParamsInterceptor())
               .addInterceptor(TokenInterceptor())
               .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
               .build()
            return Retrofit.Builder()
                .client(client)
                .baseUrl("https://baobab.kaiyanapp.com/")
                .addConverterFactory(GsonConverterFactory.create())//自动解析
                .addCallAdapterFactory(LiveDataCallAdapterFactory())//call-->Livedata
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//call--》Observable
                .build()


        }

        fun getCoroutineRetrofit():Retrofit{
            var client = OkHttpClient.Builder()
                //.retryOnConnectionFailure(true)//超时重连，默认1次，多次使用拦截器
                .cache(createCache())//配置缓存路径
                .readTimeout(30,TimeUnit.SECONDS)
                .writeTimeout(30,TimeUnit.SECONDS)
                .connectTimeout(30,TimeUnit.SECONDS)
//               .addInterceptor(RequestCacheInterceptor())//网络请求前读取缓存
//               .addNetworkInterceptor(ResponseCacheInterceptor())//网络请求后缓存数据
//               .addInterceptor(RetryInterceptor(3))
                .addInterceptor(HeaderInterceptor())
                .addInterceptor(BasicParamsInterceptor())
                .addInterceptor(TokenInterceptor())
                .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build()
            return Retrofit.Builder()
                .client(client)
                .baseUrl("https://baobab.kaiyanapp.com/")
                .addConverterFactory(GsonConverterFactory.create())//自动解析
//                .addCallAdapterFactory(LiveDataCallAdapterFactory())//call-->Livedata
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//call--》Observable
                .build()


        }
    }
}